﻿/*
 * NetArchive.h
 *
 */
#ifndef __GDK_NETARCHIVE_H__
#define __GDK_NETARCHIVE_H__

#include <gdk/core/Types.h>
#include <gdk/core/String.h>
#include <gdk/core/Archive.h>
#include <gdk/net/SimpleSocket.h>


namespace gdk
{

	class GDK_NET_EXPORT NetArchive : public Archive
	{
		DECLARE_CLASS_HANDLE(NetArchive)

	protected:
		SimpleSocket::Handle	_archiveSocket;

		String					_url;
		String					_host;
		uint16_t				_port;
		String					_path;

		ssize_t					_fileSize;
		ssize_t					_filePosition;

		String					_userName;
		String					_password;


	protected:
		NetArchive(void);
		~NetArchive(void);

	public:
		virtual ssize_t getArchiveSize(void) const;
		virtual ssize_t getPosition(void) const;
		virtual ssize_t setPosition(ssize_t pos);

	public:
		virtual ssize_t available(void) const;
		virtual ssize_t peek(void *buf, ssize_t size) const;
		virtual ssize_t read(void *buf, ssize_t size);
		virtual ssize_t write(const void *buf, ssize_t size);

	public:
		virtual bool open(const String &url, ssize_t beginPos = 0) =0;
		virtual void close(void) =0;

	public:
		void setUrl(const String &url);
		void setAuth(const String &userName, const String &password);

	public:
		__INLINE__ const String& getUrl(void) const { return _url; }
		__INLINE__ const String& getHost(void) const { return _host; }
		__INLINE__ uint16_t getPort(void) const { return _port; }
		__INLINE__ const String& getPath(void) const { return _path; }
		__INLINE__ const String& getUserName(void) const { return _userName; }
		__INLINE__ const String& getPassword(void) const { return _password; }

	public:
		static void analyUrl(const String &url, String &host, uint16_t &port, String &path);
	};

}


#endif /* __GDK_NETARCHIVE_H__ */
